package rc.memory;

import java.util.List;

import rc.Bank;

public class BankMemoryRCNotAware implements Bank
{
	public int[] accounts;

	public BankMemoryRCNotAware(List<Integer> balances)
	{
		accounts = new int[balances.size()];

		for (int i = 0; i < balances.size(); i++)
			accounts[i] = balances.get(i);
	}

	public void transfer(int from, int to, int amount)
	{
		int newBalance;

		newBalance = accounts[from];
		newBalance -= amount;

		// inconsistency, since new amount isn't saved yet

		try {
			// simulate some heavy calculations
			Thread.sleep(1000 + (int) Math.random() * 1000);
		} catch (InterruptedException e) {
		}

		accounts[from] = newBalance;

		newBalance = accounts[to];
		newBalance += amount;

		// same inconsistency

		accounts[to] = newBalance;
	}

	public void showBalances()
	{
		int total = 0;

		for (int i = 0; i < accounts.length; i++) {
			total += accounts[i];
			System.out.println("Account " + i + ": " + accounts[i]);
		}

		System.out.println("Total: " + total);
	}
}